package edu.bbu.bibliospring.backend.service.beans;

import edu.bbu.bibliospring.backend.model.Location;
import edu.bbu.bibliospring.backend.repository.LocationRepository;
import edu.bbu.bibliospring.backend.repository.exception.BibliospringRepositoryException;
import edu.bbu.bibliospring.backend.service.LocationService;
import edu.bbu.bibliospring.backend.service.exception.BibliospringServiceException;

import javax.ejb.DependsOn;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by anna on 6/12/14.
 */
@Stateless(name = "LocationService", mappedName = "ejb/LocationService")
@DependsOn({"LocationRepository", "Location"})
public class LocationServiceBean implements LocationService {

    private static final Logger LOG = Logger.getLogger(LocationService.class.getName());

    @EJB
    private LocationRepository locationRepository;

    @Override
    public List<Location> getAll() throws BibliospringServiceException {
        try {
            LOG.info("LocationService - getAll");
            return locationRepository.getAll();
        } catch (BibliospringRepositoryException e) {
            LOG.log(Level.SEVERE, e.getMessage());
            throw new BibliospringServiceException("Location repository - getAll()", e);
        }
    }

    @Override
    public Location getById(Long id) throws BibliospringServiceException {
        try {
            LOG.info("LocationService - getById");
            return locationRepository.getById(id);
        } catch (BibliospringRepositoryException e) {
            LOG.log(Level.SEVERE, e.getMessage());
            throw new BibliospringServiceException("Location repository - getById()", e);
        }
    }

    @Override
    public Location insertLocation(Location location) throws BibliospringServiceException {
        try {
            LOG.info("LocationService - insertLocation");
            return locationRepository.save(location);
        } catch (BibliospringRepositoryException e) {
            LOG.log(Level.SEVERE, e.getMessage());
            throw new BibliospringServiceException("Location repository - insertLocation()", e);
        }
    }
}
